package softtech.app.secretcaller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import softtech.app.secretcaller.AdapterCallLog.ViewHolder;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdapterCallLog extends ArrayAdapter<DatabaseCall> {

	List<DatabaseCall> mListCall;
	Context mContext;
	
	DatabaseHandler db;
	
	private int mode_load;
	
	private int USER_ID;
	
	public AdapterCallLog(Context ctx ,  int textViewResourceId , ArrayList<DatabaseCall> list){
		super(ctx, textViewResourceId);
		db = new DatabaseHandler(ctx);
		mListCall = list;
		mContext = ctx;
		mode_load = 1;
		USER_ID = checkLogin(ctx);
	}

	public int getMode_load() {
		return mode_load;
	}

	public void setMode_load(int mode_load) {
		this.mode_load = mode_load;
	}

	@Override
	public int getCount() {
		if(mListCall==null || mListCall.equals(null) || mListCall.size() == 0){
			return 1;
		}
		
		return mListCall.size();
	}

	@Override
	public DatabaseCall getItem(int position) {
		if(mListCall==null || mListCall.equals(null) || mListCall.size() == 0){
			return null;
		}
		return mListCall.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(parent.getContext()==null){		
			return null;
		}
		
		if(mListCall.size()==0){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,parent, false);
			return convertView;
		}
		
		ViewHolder viewHolder=null;
		if(convertView==null){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.layout_call_item,parent, false);
			
			viewHolder=new ViewHolder();
			viewHolder.tvNameContact	= (TextView) convertView.findViewById(R.id.tvNameContact);
			viewHolder.tvLastTime = (TextView) convertView.findViewById(R.id.tvLastTime);
			viewHolder.imgTypeAvatar	= (ImageView) convertView.findViewById(R.id.imgTypeAvatar);
			viewHolder.imgDelete			= (ImageView) convertView.findViewById(R.id.imgDelete);
			viewHolder.linearCalendar 	= (LinearLayout) convertView.findViewById(R.id.linearCalendar);
			viewHolder.tvCalendarDate	= (TextView) convertView.findViewById(R.id.tvCalendarDate);
			
						
			convertView.setTag(viewHolder);
			convertView.setTag(R.id.tvNameContact,viewHolder.tvNameContact);
			convertView.setTag(R.id.tvLastTime,viewHolder.tvLastTime);
			convertView.setTag(R.id.imgTypeAvatar , viewHolder.imgTypeAvatar );
			convertView.setTag(R.id.imgDelete, viewHolder.imgDelete);
			convertView.setTag(R.id.linearCalendar, viewHolder.linearCalendar);
			convertView.setTag(R.id.tvCalendarDate , viewHolder.tvCalendarDate);
			
			
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		if(mListCall.get(position).getContact_id() != -1){
			DatabaseContact contact = db.getContact( (int) mListCall.get(position).getContact_id(), USER_ID);
			try{
				
				Log.e("contact" , contact.toJSONString());
				viewHolder.tvNameContact.setText(contact.getContactName());
			}catch(Exception e){
				contact = db.getContactLikePhoneNumber(mListCall.get(position).getPhone(), USER_ID);
				mListCall.get(position).setContact_id(contact.get_Id());
				db.updateCaller(mListCall.get(position));
				if(ToolsSaveFile.checkStringNotNull(contact.getContactName())){
					viewHolder.tvNameContact.setText(contact.getContactName());
				}else{
					viewHolder.tvNameContact.setText(mListCall.get(position).getPhone());
				}
				
			}
			
			
		}else{
			viewHolder.tvNameContact.setText(mListCall.get(position).getPhone());
		}
		
		viewHolder.tvLastTime.setText(mListCall.get(position).getDate().substring(10 , 16));
		if( mListCall.get(position).getType() == DatabaseCall.INCOMMING ){
			viewHolder.tvLastTime.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_in_comming_call, 0, 0, 0);
		}else if(mListCall.get(position).getType() == DatabaseCall.OUTGOING){
			viewHolder.tvLastTime.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_out_going_call, 0, 0, 0);
		}else {
			viewHolder.tvLastTime.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_in_comming_call, 0, 0, 0);
		}
		if(mListCall.get(position).getIsFirstCallOfDay()){
			viewHolder.linearCalendar.setVisibility(View.VISIBLE);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd",java.util.Locale.getDefault());
			String today = sdf.format(new Date());
			if(mListCall.get(position).getDate().substring(0, 10).equals(today)){
				viewHolder.tvCalendarDate.setText(mContext.getResources().getString(R.string.today));
			}else{
				viewHolder.tvCalendarDate.setText(mListCall.get(position).getDate().substring(0, 10));
			}
		}else{
			viewHolder.linearCalendar.setVisibility(View.GONE);
		}
		viewHolder.imgDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActivityContactList.mFlagCalling = ActivityContactList.FLAG_CALLING;
				ListenerPhoneState.isInAppDelete = true;
				ListenerPhoneState.phone_number = mListCall.get(position).getPhone();
				ControlPhoneAction.makeCall(mContext, mListCall.get(position).getPhone());
				
			}
		});
		return convertView;
	}

	static class ViewHolder{
		TextView tvNameContact , tvLastTime;
		ImageView imgTypeAvatar;
		ImageView imgDelete;
		LinearLayout linearCalendar;
		TextView tvCalendarDate;
	}
	
	/**
	 * Check Login - (-1) : if not login. - id_login : if logged in.
	 */
	int checkLogin(Context context) {
		int id = -1;
		ToolsSaveFile tsf = new ToolsSaveFile();

		String id_loggedin = tsf.LoadContentFile(context,
				ConstantsFileName.FILE_ID_LOG_IN);

		if (id_loggedin == null || id_loggedin.equals("")
				|| id_loggedin.length() == 0) {

			id = -1;
		} else {
			try {
				id = Integer.parseInt(id_loggedin);

			} catch (Exception e) {
				id = -1;
				e.printStackTrace();
			}
		}

		return id;
	}
}